package com.huiztech.maystall.activity;

import com.huiztech.maystall.ui.FlowLayout;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class ProductDetailActivity extends BaseActivity {

    private Button backBtn, goshoppingBtn,moreImageBtn;
    private ImageButton homeBtn;
    private LinearLayout commentLayout, likeLayout, shareLayout, shoppingLayout, viLayout, colorLayout, sizeLayout;
    FlowLayout layout, layout2;// 流式布局 颜色 尺码

    private ImageView likeImageView;
    private ImageView detailImageView;
    private boolean isLike = false;
    private ImageView pullUpImage, pullDownImage;
    private boolean isVisible = false;
    private AlertDialog dialog;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_detail);

        viLayout = (LinearLayout) findViewById(R.id.product_detail_vi_layout);
        backBtn = (Button) findViewById(R.id.product_detail_back_btn);
        homeBtn = (ImageButton) findViewById(R.id.product_detail_backhome_btn);
        commentLayout = (LinearLayout) findViewById(R.id.product_detail_comment_layout);
        likeLayout = (LinearLayout) findViewById(R.id.product_detail_like_layout);
        shareLayout = (LinearLayout) findViewById(R.id.product_detail_share_layout);
        shoppingLayout = (LinearLayout) findViewById(R.id.product_detail_shopping_layout);
        likeImageView = (ImageView) findViewById(R.id.product_detail_like_imageView);
        layout = (FlowLayout) findViewById(R.id.flowLayout);
        layout2 = (FlowLayout) findViewById(R.id.flowLayout2);
        pullUpImage = (ImageView) findViewById(R.id.product_detail_pull_up);
        pullDownImage = (ImageView) findViewById(R.id.product_detail_pull_down);
        colorLayout = (LinearLayout) findViewById(R.id.product_detail_color_layout);
        sizeLayout = (LinearLayout) findViewById(R.id.product_detail_size_layout);
        goshoppingBtn = (Button) findViewById(R.id.product_detail_goshopping_btn);
        detailImageView = (ImageView) findViewById(R.id.product_detail_image);
        moreImageBtn = (Button) findViewById(R.id.browse_more_btn);
        
        detailImageView.setBackgroundResource(Integer.valueOf(getIntent().getStringExtra("image")));
        
        initColorButton();// 初始化颜色尺寸按钮
        
        moreImageBtn.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ProductDetailActivity.this, MoreImageActivity.class);
                startActivity(intent);
            }
        });

        goshoppingBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                addSuccess();
            }
        });

        pullUpImage.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                viLayout.setVisibility(View.VISIBLE);
                isVisible = true;
            }
        });

        pullDownImage.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                viLayout.setVisibility(View.GONE);
                isVisible = false;
            }
        });

        backBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
        homeBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ProductDetailActivity.this, HomeActivity.class);
                startActivity(intent);
                finish();
            }
        });

        commentLayout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ProductDetailActivity.this, CommentActivity.class);
                startActivity(intent);
            }
        });

        likeLayout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (isLike) { // 标记为喜欢的情况
                    isLike = false;
                    likeImageView.setBackgroundResource(R.drawable.product_detail_shixin);
                } else { // 标记为不喜欢的情况
                    isLike = true;
                    likeImageView.setBackgroundResource(R.drawable.product_detail_xin_shen);
                }
            }
        });

        shareLayout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                //分享
            }
        });

        shoppingLayout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (isVisible) {
                    viLayout.setVisibility(View.GONE);
                    isVisible = false;
                } else {
                    viLayout.setVisibility(View.VISIBLE);
                    isVisible = true;
                }
            }
        });
    }

    private void initColorButton() {
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int widthWindow = dm.widthPixels;
        int width = (int) (widthWindow * 0.8);

        final Button Btn[] = new Button[5];
        int j = -1;
        for (int i = 0; i <= 4; i++) {
            if (i % 5 <= 2) {
                Btn[i] = new Button(this);
                Btn[i].setId(2000 + i);
                Btn[i].setText("蓝色");
                RelativeLayout.LayoutParams btParams = new RelativeLayout.LayoutParams(width / 3,
                        RelativeLayout.LayoutParams.WRAP_CONTENT); // 设置按钮的宽度和高度
                if (i % 4 == 0) {
                    j++;
                }
                Btn[i].setBackgroundResource(R.drawable.product_detail_nocheck_btn_bg);
                layout.addView(Btn[i], btParams); // 将按钮放入layout组件
            } else {
                Btn[i] = new Button(this);
                Btn[i].setId(2000 + i);
                Btn[i].setText("绿色");
                RelativeLayout.LayoutParams btParams = new RelativeLayout.LayoutParams(width / 2,
                        RelativeLayout.LayoutParams.WRAP_CONTENT); // 设置按钮的宽度和高度
                if (i % 4 == 0) {
                    j++;
                }
                Btn[i].setBackgroundResource(R.drawable.product_detail_nocheck_btn_bg);
                layout.addView(Btn[i], btParams); // 将按钮放入layout组件
            }
        }

        for (int k = 0; k <= Btn.length - 1; k++) {
            // 这里不需要findId，因为创建的时候已经确定哪个按钮对应哪个Id
            Btn[k].setTag(k); // 为按钮设置一个标记，来确认是按下了哪一个按钮

            Btn[k].setOnClickListener(new Button.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int i = (Integer) v.getTag();
                    Btn[i].setBackgroundResource(R.drawable.product_detail_check_btn_bg);
                    for (int k2 = 0; k2 < Btn.length; k2++) {
                        if (k2 != i) {
                            Btn[k2].setBackgroundResource(R.drawable.product_detail_nocheck_btn_bg);
                        }
                    }
                }
            });
        }
        initSizeButton();
    }

    private void initSizeButton() {
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int widthWindow = dm.widthPixels;
        int width = (int) (widthWindow * 0.8);

        final Button Btn[] = new Button[5];
        int j = -1;
        for (int i = 0; i <= 4; i++) {
            if (i % 5 <= 2) {
                Btn[i] = new Button(this);
                Btn[i].setId(2000 + i);
                Btn[i].setText("XL");
                RelativeLayout.LayoutParams btParams = new RelativeLayout.LayoutParams(width / 3,
                        RelativeLayout.LayoutParams.WRAP_CONTENT); // 设置按钮的宽度和高度
                if (i % 4 == 0) {
                    j++;
                }
                Btn[i].setBackgroundResource(R.drawable.product_detail_nocheck_btn_bg);
                layout2.addView(Btn[i], btParams); // 将按钮放入layout组件
            } else {
                Btn[i] = new Button(this);
                Btn[i].setId(2000 + i);
                Btn[i].setText("M");
                RelativeLayout.LayoutParams btParams = new RelativeLayout.LayoutParams(width / 2,
                        RelativeLayout.LayoutParams.WRAP_CONTENT); // 设置按钮的宽度和高度
                if (i % 4 == 0) {
                    j++;
                }
                Btn[i].setBackgroundResource(R.drawable.product_detail_nocheck_btn_bg);
                layout2.addView(Btn[i], btParams); // 将按钮放入layout组件
            }
        }

        for (int k = 0; k <= Btn.length - 1; k++) {
            // 这里不需要findId，因为创建的时候已经确定哪个按钮对应哪个Id
            Btn[k].setTag(k); // 为按钮设置一个标记，来确认是按下了哪一个按钮

            Btn[k].setOnClickListener(new Button.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int i = (Integer) v.getTag();
                    Btn[i].setBackgroundResource(R.drawable.product_detail_check_btn_bg);

                    for (int k2 = 0; k2 < Btn.length; k2++) {
                        if (k2 != i) {
                            Btn[k2].setBackgroundResource(R.drawable.product_detail_nocheck_btn_bg);
                        }
                    }

                }
            });
        }
        viLayout.setVisibility(View.GONE); // 隐藏布局
    }

    private void addSuccess() {
        dialog = new AlertDialog.Builder(ProductDetailActivity.this).create();
        dialog.show();
        dialog.getWindow().setContentView(R.layout.add_success_dialog);
        dialog.getWindow().findViewById(R.id.add_success_show_cart).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ProductDetailActivity.this, ShoppingCartActivity.class);
                startActivity(intent);
                dialog.dismiss();
            }
        });

        dialog.getWindow().findViewById(R.id.add_success_look).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }

}
